package com.yonder.testjvmmemory;

public class TestObjectMemory {

	static Object[] arr = null;
	public static void main(String[] args) throws Exception {
		int n = 1000000;
		long initM = printRuntime();
		arr = new Object[n];
		System.gc();
		long arrInitM = printRuntime();
		System.out.println("init arr use:" + (arrInitM - initM));
		for (int i = 0; i < arr.length; i++) {
			arr[i] = new Object();
		}
		System.gc();
		long objInitM = printRuntime();
		System.out.println("init arr use:" + (objInitM - arrInitM));
		System.out.println("single role_base use:" + (objInitM - arrInitM)/n);
	}

	private static long printRuntime() throws Exception {
	    Runtime r = Runtime.getRuntime();
	    long useMemory = (r.totalMemory() - r.freeMemory());
	    System.out.println("JVM已使用内存:    " + (r.totalMemory() - r.freeMemory()));
	//    System.out.println("JVM可以使用的总内存:    " + r.totalMemory());
	//    System.out.println("JVM可以使用的剩余内存:    " + r.freeMemory());
	    return useMemory;
	}
}
